$(function() {
	
	editLink({
		id:CD.geturl().id
	},function(data){
		$('#editLink').html(template('editTemplate',data));
		//打开新图标
	$('.createNew').on('tap', function() {
		$('.choose-newicon').stop().animate({
			"left": "0"
		});
	});
	//关闭新图标
	$('#newback').on('tap', function() {
		$('.choose-newicon').stop().animate({
			"left": "100%"
		});
	});

	//选择图标
	$('.seleteIcon li').on('click', function(){
		$(this).css({
			"opacity": "1",
			"border": "1px solid #999"
		});
		$(this).siblings().css({
			"opacity": "0.3"
		});

		var iconWord = $(this).children().eq(0).attr('class');
		console.log(iconWord);

		//替换图标及背景颜色
		$('#newfinish').on('tap', function() {
			$('.choose-newicon').animate({
				"left": "100%"
			});
			$('.createNew').children().attr('class', iconWord);
			var rand = '#' + random1(6);
			$('.createNew').children().css({
				"background-color": rand
			});
		});
	});
	});
	
	$('.code-btn').on('tap',function(){
		var numOne = $('.createNew').children().css('background-color');
		var numTwo = $('.createNew').children().attr('class');
		var numThree = $('.iconname').children().val();
		if(!numThree){
			mui.toast('栏目不能为空!');
			return false;	
		}; 
		var numFour = $('.iconlink').children().val();
		console.log(numFour)
		if(!numFour){
			mui.toast('链接地址不能为空!');
			return false;
		};
		keepLink({
			id:CD.geturl().id,
			linkColour:numOne,
			linkImg:numTwo,
			linkName:numThree,
			linkSite:numFour
		},function(data){
			if(data==1){
				mui.toast('修改成功!')
			}else{
				mui.toast('系统繁忙!')
			}
			
		})
	});

	//反正手机键盘破坏布局
	var win_h = $(window).height(); //关键代码
	window.addEventListener('resize', function() {
		if($(window).height() < win_h) {
			$('.code-btn').hide();
		} else {
			$('.code-btn').show();
		}
	});
	

	
	
	
	

});

//随机数
function random1(len) {
        var pwd = "";
        for(var idx = 0; idx < len; idx ++){
            pwd = pwd + ((Math.random() * 16) & (0x5 | 0x9)).toString(16);
        }
        return pwd;
    }

//修改遍历
var editLink = function(params,callback){
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/link/selectById",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data:params,
		success: function(data) {
			//console.log(data);
			callback && callback(data);
		}
	});
};

//保存修改
var keepLink = function(params,callback){
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/link/update",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data:params,
		success: function(data) {
			//console.log(data);
			callback && callback(data);
		}
	});
}



